package com.zsw.recongize.frame;

import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.zsw.recongize.parse.PdfBoxParse;

public class JFrame_ extends JFrame{
	private static final long serialVersionUID = -3546627714619628765L;
	
	public JFrame_() {
		Image img = new ImageIcon(this.getClass().getResource("/invoice.jpg")).getImage();
		this.setIconImage(img);
		this.setSize(500, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.init();
	}
	
	public void init(){
		setTitle("Hello Girl");
		JPanel contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		final JFileChooser jfc = new JFileChooser();
		JLabel label =new JLabel("源目录或文件:");
		label.setBounds(10, 10, 90, 30);
		contentPane.add(label);
		
		final JTextField text_0 =new JTextField();
		text_0.setBounds(100, 10, 300, 30);
		contentPane.add(text_0);
		text_0.setEditable(false);
		
		JButton button_0 =new JButton("选择");
		button_0.setBounds(410, 10, 60, 30);
		contentPane.add(button_0);
		button_0.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				int r = jfc.showOpenDialog(null);
				if(r == JFileChooser.APPROVE_OPTION){
					String path = jfc.getSelectedFile().getAbsolutePath();
					text_0.setText(path);
				}
			}
		});
		
		JLabel label_1 = new JLabel("结果目录:");
		label_1.setBounds(10, 50, 90, 30);
		contentPane.add(label_1);
		
		final JTextField textField = new JTextField();
		textField.setBounds(100, 50, 300, 30);
		textField.setEditable(false);
		contentPane.add(textField);
		
		JButton button_1 = new JButton("选择");
		button_1.setBounds(410, 50, 60, 30);
		contentPane.add(button_1);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int r = jfc.showOpenDialog(null);
				if(r == JFileChooser.APPROVE_OPTION){
					String path = jfc.getSelectedFile().getAbsolutePath();
					textField.setText(path);
				}
			}
		});
		JLabel label_2 = new JLabel("名称:");
		label_2.setBounds(10, 90, 90, 30);
		contentPane.add(label_2);
		final JTextField textField_1 = new JTextField();
		textField_1.setBounds(100, 90, 300, 30);
		contentPane.add(textField_1);
		
		JLabel label_3 = new JLabel("税号:");
		label_3.setBounds(10, 130, 90, 30);
		contentPane.add(label_3);
		final JTextField textField_2 = new JTextField();
		textField_2.setBounds(100, 130, 300, 30);
		contentPane.add(textField_2);
		
		JButton button_2 = new JButton("识别");
		button_2.setBounds(70, 222, 113, 30);
		contentPane.add(button_2);
		button_2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if("".equals(text_0.getText())){
					JOptionPane.showMessageDialog(null, "请选择源目录或文件", "错误提示",JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				if("".equals(textField.getText())){
					JOptionPane.showMessageDialog(null, "请选择结果目录", "错误提示",JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				String result = PdfBoxParse.pase(text_0.getText(), textField.getText(),
						textField_1.getText().trim(), textField_2.getText().trim());
				
				JOptionPane.showMessageDialog(null, "结果文件位置：" + result, "提示",JOptionPane.INFORMATION_MESSAGE);
			}
		});
		
		JButton button_3 = new JButton("关闭");
		button_3.setBounds(210, 222, 113, 30);
		contentPane.add(button_3);
		button_3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		JButton button_4 = new JButton("真伪验证");
		button_4.setBounds(350, 222, 113, 30);
		contentPane.add(button_4);
		button_4.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(java.awt.Desktop.isDesktopSupported()){
					try{
						java.net.URI uri=java.net.URI.create("http://localhost:520/mm/index_.html");
						java.awt.Desktop dp=java.awt.Desktop.getDesktop();
						if(dp.isSupported(java.awt.Desktop.Action.BROWSE)){
							dp.browse(uri);
						}
					}catch(Exception e1){
					}
				}
			}
		});
		
		JLabel lblNewLabel = new JLabel("说明:源目录或文件指要识别的pdf文件或所在文件夹");
		lblNewLabel.setFont(new Font("宋体",Font.BOLD, 14));
		lblNewLabel.setBounds(10, 170, 460, 30);
		contentPane.add(lblNewLabel);
		JLabel lblNewLabel2 = new JLabel("结果目录指识别文件后生成的excel文件存放位置");
		lblNewLabel2.setFont(new Font("宋体",Font.BOLD, 14));
		lblNewLabel2.setBounds(52, 195, 418, 30);
		contentPane.add(lblNewLabel2);
	}
}
